import pymysql
import KBase


def updatekbasehfsid():
    sql="select book_id from books_1100 where `index` is null"
    cur1.execute(sql)
    ids=cur1.fetchall()
    for id in ids:
        book_id=id[0]
        hfs_id=book_id+"_null"
        update="update BOOKS_1100 set HFSID='"+hfs_id+"' where BOOK_ID='"+book_id+"'"
        cur_kbase.execute(update)
        conn_kbase.commit()

def updatehfsidwithindex():
    sql="select id,book_id,`index` from books_700 "
    cur1.execute(sql)
    datas=cur1.fetchall()
    values=[]
    for data in datas:
        id=data[0]
        book_id=data[1]
        index=data[2]
        if index==None:
            hfs_id=book_id+"_null"
            update="update BOOKS_700 set HFSID='"+hfs_id+"' where ID="+str(id)
        else:
            hfs_id=book_id+"_"+str(index)
            update="update BOOKS_700 set HFSID='"+hfs_id+"' where ID="+str(id)
        print(update)
        cur_kbase.execute(update)
        conn_kbase.commit()


if __name__ == '__main__':
    try:
        conn1 = pymysql.Connect(host = '10.170.128.56',
                                port = 3306,
                                user = 'root',
                                passwd = '123456',
                                db = 'books',
                                charset='utf8mb4')
        cur1 = conn1.cursor()

        conn_kbase = KBase.connect(host="192.168.105.89",
                                   port=4567,
                                   user="DBOWN",
                                   passwd="")
        cur_kbase = conn_kbase.cursor()
        updatehfsidwithindex()

    finally:
        cur1.close()
        conn1.close()
        cur_kbase.close()
        conn_kbase.close()